import csv
from django.http import HttpResponse
from django.shortcuts import render
import random
from random import choice
def download(request):
	response = HttpResponse(content_type = 'text/csv')
	response['Conten-Disposition'] = 'attachment;filename = "somefilename.csv"'
	writer = csv.writer(response)
	writer.writerow(['First row','A','B','C'])
	return response

# 设置首页
def main_view(request):
	return render(request,'main.html')
# 登录页面
def login_view(request):
	return render(request,'login.html')
# 获取登录账号信息
#def getUserInfo_view(request):

# 登录后跳转到teacher账号页面
def teacher_view(request):
	topicNum=5#默认题目个数
	maxNum =100
	minNum =1
	if request.method == "GET":

		return render(request,'teacher.html')
	
	if request.method == "POST":
		topicNum = int(request.POST['topicNum'])
		maxNum = int(request.POST['maxNum'])
		minNum = int(request.POST['minNum'])

		

	sumTopic = []
	global sumResult 
	sumResult = []
	for i in range(topicNum):
		if i % 5==0:
			sumTopic.append("br")
		#0~100的四则运算
		oneNum = random.randint(minNum,maxNum)
		twoNum = random.randint(minNum,maxNum)
		opSymbol = ["+","-","×","÷"]
		liSum = ["@","@","@","@"]
		opSy = choice(opSymbol)
		if opSy == "+":
			ans = oneNum + twoNum
			
		elif opSy == "-":
			ans = oneNum - twoNum
			
		elif opSy == "×":
			ans = oneNum*twoNum
			
		elif opSy == "÷":
			ans = "%.2f"%(oneNum/twoNum)
		else:
			pass
		sumResult.append(str(ans))
		
		atic = "("+str(i+1)+")"+" "+str(oneNum)+opSy+str(twoNum)+"="
		sumTopic.append(atic)

	# print(sumTopic)
	return render(request,'teacher.html',locals())
def qingqiu(request):
	if request.method == "GET":
		res = request.GET.getlist('res','')
		recordFault =[]
		if res == sumResult:
			ans="答案正确！"
		else:
			ans="很遗憾，你没有全部做对！"
			for i in range(len(sumResult)):
				if res[i] == sumResult[i]:
					pass
				else:
					recordFault.append(i+1)




		# if res == sumResult:
		# 	ans="答案正确！"
		# else:
		# 	ans="很遗憾，你没有全部做对！"
		#return render(request,'teacher.html')
		#return HttpResponse("在/qingqiu页面get请求也是结果页面，页面内容不变")
	elif request.method == "POST":
		res = request.POST.getlist('res','')
		recordFault =[]
		if res == sumResult:
			ans="答案正确！"
		else:
			ans="很遗憾，你没有全部做对！"
		# return render(request,'teacher.html',locals())
		# return HttpResponse("跳转到结果页面")
			for i in range(len(sumResult)):
				if res[i] == sumResult[i]:
					pass
				else:
					recordFault.append(i+1)

	return render(request,'result.html',locals())
	

# 提交答案后跳出学生成绩和对应的错题
# def stuScore(request):
def test_mycal(request):

    if request.method == 'GET':
        return render(request, 'mycal.html')
    elif request.method == 'POST':
        #处理计算
        x = int(request.POST['x'])
        y = int(request.POST['y'])
        op = request.POST['op']

        result = 0
        if op == 'add':
            result = x + y
        elif op == 'sub':
            result = x - y
        elif op == 'mul':
            result = x * y
        elif op == 'div':
            result = x / y

        #dic={'x':x, 'y':y, 'op':op}
        return render(request, 'mycal.html', locals())